package com.ai.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.ai.common.model.dto.HtmlParamDTO;
import com.ai.common.utils.lang.json.JsonUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * @author daizy
 * 
 * html生成用具类
 * 
 * 2015/06/16 14:00:00
 *
 */
@Service("htmlCreaterUtils")
public class HtmlCreaterUtils {
	
	private final static Log logger = LogFactory.getLog(HtmlCreaterUtils.class);
	
	 /** 编码格式 */
    private static final String ENCODING = "UTF-8";
    
    /**
     * 生成静态压面
     * @param HtmlParamDTO
     */
    @SuppressWarnings("unchecked")
	public static void createHtmlByTemplate(HtmlParamDTO dto) {
    	String htmlPath = dto.getHtmlPath();
    	File dirFile = new File(htmlPath);
        //判断是否存在重名
        if(!dirFile.exists()) {
            dirFile.mkdirs();
        }
        try {
            // 设置格式
        	Configuration config = new Configuration();
            config.setDirectoryForTemplateLoading(new File(dto.getTemplatePath()));
            config.setEncoding(Locale.CHINA, ENCODING);
            // 设置模版动态参数
            Map<String, Object> root = new HashMap<String, Object>();
            Map<String, Object> data = JsonUtils.fromJson(dto.getJsonData(), Map.class);
            data.put("miscDomain", dto.getMiscDomain());
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
            data.put("timeInfo", sdf.format(dto.getDateTime()));
            root.put("data", data);
            Template template = config.getTemplate(dto.getTemplateName());
            //建html文件
            StringBuffer htmlUrl = new StringBuffer();
            htmlUrl.append(htmlPath+"/").append(dto.getPageName()).append(".html");
            System.out.println("html路径："+htmlUrl.toString());
            Writer out = new OutputStreamWriter(new FileOutputStream(htmlUrl.toString()), ENCODING);
            // 数据录入
            template.process(root, out);
        } catch (Exception e) {
        	logger.error("生成静态页面异常："+e.getMessage());
        }
    }
    
    
	public static void main(String[] args) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "1");
		map.put("title", "测试标题");
		// 模版路径
		String templatePath = "F:/daizhaoyun/selfSpace/info-backend/info-backend-server/src/main/webapp/WEB-INF/view/HtmlTemplate/";
		// 模版名称
		String templateName = "1.ftl";
		// html存放路径
		String htmlPath = "F:/daizhaoyun/selfSpace/info-backend/info-backend-server/src/main/webapp/statics/zxHtml/publish_11/20150901";
		HtmlParamDTO dto = new HtmlParamDTO();
		dto.setHtmlPath(htmlPath);
		dto.setJsonData(JsonUtils.toJson(map));
		dto.setTemplateName(templateName);
		dto.setTemplatePath(templatePath);
		HtmlCreaterUtils.createHtmlByTemplate(dto);
	}

}
